using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using My.IdlConverter.Core.Filters;
using My.IdlConverter.Core;

namespace My.IdlConverter.Com
{
    using Path = System.IO.Path;

    class ComMethodFilter : IMethodFilter
    {
        public virtual bool Match(string name)
        {
            throw new NotImplementedException("The match method is no use in the class");
        }

        public virtual bool Match(Method method)
        {
            string filterFile = My.IdlConverter.Core.Cfg.Configuration.FilterSettingPath;
            XmlDocument doc = new XmlDocument();
            doc.Load(filterFile);
            XmlNode node = doc.SelectSingleNode("//filters/method_filters");
            XmlNodeList methodFilters = node.SelectNodes(".//method_filter");
            foreach (XmlNode nextMethodFilter in methodFilters)
            {
                XmlAttribute nameAttr = nextMethodFilter.Attributes["name"];
                XmlNode interfaceNameNode = nextMethodFilter.SelectSingleNode(".//interface_name");
                if (method.Name.Equals(nameAttr.Value)
                    && method.Parent.Name.Equals(interfaceNameNode.InnerText))
                {
                    XmlNode paramCountNode = nextMethodFilter.SelectSingleNode(".//param_count");
                    if (paramCountNode != null)
                    {
                        if (method.Parameters.Count == Int32.Parse(paramCountNode.InnerText))
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    return true;
                }
            }
            
            return false;
        }
    }
}
